<template>
  <div class="heard">
    <el-tabs v-model="listParams.type" @tab-click="handleClick">
      <el-tab-pane label="9001优势" name="2"></el-tab-pane>
      <el-tab-pane label="学习流程" name="3"></el-tab-pane>
      <el-tab-pane label="招生就业" name="4"></el-tab-pane>
      <el-tab-pane label="客户群体" name="5"></el-tab-pane>
      <el-tab-pane label="关于9001" name="6"></el-tab-pane>
      <el-tab-pane label="与商学院合作的三把利剑" name="7"></el-tab-pane>
      <el-tab-pane label="合作形式" name="8"></el-tab-pane>
      <el-tab-pane label="前期合作申请流程" name="9"></el-tab-pane>
      <el-tab-pane label="友情链接" name="10"></el-tab-pane>
      <el-tab-pane label="平台协议" name="11"></el-tab-pane>
      <el-tab-pane label="内审员培训协议" name="12"></el-tab-pane>
       <el-tab-pane label="四大理由" name="13"></el-tab-pane>
    </el-tabs>
    <!-- list -->
    <div v-if="listParams.type == '2'||listParams.type == '3'||listParams.type == '4'||listParams.type == '5'||listParams.type == '6'||listParams.type == '7'||listParams.type == '8'||listParams.type == '9'||listParams.type == '10'||listParams.type == '11'||listParams.type == '12'||listParams.type == '13'">
      <el-form v-if="listParams.type != '11' && listParams.type != '12'">
        <el-form-item label="模块标题">
          <el-input v-model="formT.title"></el-input>
        </el-form-item>
        <el-form-item v-if="listParams.type == '4'" label="副标题">
          <el-input v-model="formT.subtitle"></el-input>
        </el-form-item>
      </el-form>
      <div v-if="listParams.type != '11' && listParams.type != '12'" style="text-align:center;margin:10px">
        <el-button v-if="listParams.type == '10'" size="small" @click="add" type="" >新增</el-button>
        <el-button size="small" @click="saveT" type="warning" >保存</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          v-if="listParams.type != '6'"
          prop="content"
          label="标题"
          align="center"
          min-width="150"
        ></el-table-column>
        <!-- <el-table-column
          v-if="listParams.type = '10'"
          prop="content"
          label="名称"
          align="center"
          min-width="150"
        ></el-table-column> -->
        <el-table-column
          v-if="listParams.type == '6'||listParams.type == '10'||listParams.type == '13'"
          prop="introduce"
          label="介绍"
          align="center"
          min-width="150"
        ></el-table-column>
        <el-table-column
          label="操作"
          min-width="230"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              v-if="listParams.type == '10'"
              icon="el-icon-edit"
              @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
       <!-- 编辑 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form label-width="80px" :model="form">
          <el-form-item v-if="listParams.type != '8'&& listParams.type != '10'&& listParams.type != '11'&& listParams.type != '12'&& listParams.type != '13'" label="图片">
            <el-upload
              :headers="headers"
              :action="`${baseUrl}/mba/course/upload`"
              :show-file-list="false"
              :on-success="uploadLogoPicUrl"
              class="avatar-uploader"
              accept=".jpg,.jpeg,.png,.gif"
            >
              <img
                v-if="form.logo"
                :src="showUrl + form.logo"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item v-if="listParams.type == '6'" label="视频">
            <el-upload
              :headers="headers"
              :action="`${baseUrl}/mba/course/upload`"
              :show-file-list="false"
              :on-success="uploadLogoPicUrlV"
              class="avatar-uploader"
              accept=".jpg,.jpeg,.png,.gif"
            >
              <video style="width:300px;height:200px" v-if="form.video" :src="showUrl + form.video" controls="controls">
                您的浏览器不支持 video 标签
              </video>
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item v-if="listParams.type != '6'" label="标题">
            <el-input v-model="form.content"></el-input>
          </el-form-item>
          <el-form-item v-if="listParams.type == '10'" label="排序">
            <el-input-number v-model="form.sort"></el-input-number>
          </el-form-item>
          <el-form-item v-if="listParams.type != '5' && listParams.type != '11'&& listParams.type != '12'" label="内容">
            <el-input type="textarea" autosize v-model="form.introduce"></el-input>
          </el-form-item>
          <el-form-item label="内容"  v-if="listParams.type == 11">
            <Editor v-model="form.introduce" />
          </el-form-item>
          <el-form-item label="培训协议"  v-if="listParams.type == 12">
            <Editor v-model="form.introduce" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
          <el-button size="small" type="primary" @click="save()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {getList ,edit,add,del} from '@/api/web/change'
import { getToken } from "@/utils/auth";
import Editor from "@/components/Editor";
export default {
   components: { Editor },
  data() {
    return{
      headers: { Authorization: "Bearer " + getToken() },
      baseUrl: process.env.VUE_APP_BASE_API,
      // 标签页
      listParams:{
        type:'2',
        status:2
      },
      // 加载
      loading:false,
      // 标题表单
      formT:{
        id:'',
        title:''
      },
      title:'',
      // list编辑
      form:{
        content: "",
        id: "",
        introduce: "",
        logo: "",
        status: "",
        subtitle: "",
        title: "",
        type: "",
        video: "",
      },
      form2:{
        type:'12',
        status:2
      },
      // list列表
      tableData:[],
      dialogFormVisible:false,
    }
  },
  created(){
    this.getTitle()
    this.getList()
  },
  methods:{
    add(){
      this.title = '新增'
      this.form = {
        content:'',
        introduce:'',
        sort:'',
        type:this.listParams.type
      }
      this.dialogFormVisible = true
    },
    // 选择
    handleClick(tab) {
        this.getTitle()
        this.getList()
    },
    // 打开编辑
    handleEdit(row) {
      this.title = '编辑'
      this.$nextTick(() => {
        this.form = row;
        })
      // this.form = row
      this.dialogFormVisible = true
    },
    // 上传图片
    uploadLogoPicUrl(res) {
      this.form.logo = res.data;
    },
    // 上传视频
    uploadLogoPicUrlV(res) {
      this.form.video = res.data;
    },
    del(row) {
      this.$confirm('此操作将删除该链接, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          del(row.id).then(res => {
            if(res.code == 200){
              this.getList()
              this.$message.success('删除成功')
            }
          })
        }).catch(() => {
            this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    // 保存
    saveT() {
      // console.log(this.formT)
      edit(this.formT).then(res => {
        if(res.code == 200){ 
          this.$message.success('保存成功')
          this.getTitle()
        }
      })
    },
    // 保存list
    save() {
      if(this.title == '编辑'){
        edit(this.form).then(res => {
          if(res.code == 200){ 
            this.$message.success('保存成功')
            this.dialogFormVisible = false
            this.getList()
          }
        })
      }else if(this.title == '新增'){
        add(this.form).then(res => {
          if(res.code == 200){ 
            this.$message.success('保存成功')
            this.dialogFormVisible = false
            this.getList()
          }
        })
      }
    },
    
    // 获取标题
    getTitle() {
      let data = {
        type :this.listParams.type,
        status:1
      }
      getList(data).then(res => {
        if(res.code == 200){
          this.formT = res.rows[0]
        }
      })
    },
    // list请求
    getList() {
      getList(this.listParams).then(res => {
        if(res.code == 200){
          this.tableData = res.rows
        }
      })
    },
  }
}
</script>

<style >
  .heard{
    padding: 20px 20px 40px 20px;
  }
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 200px;
    height: 200px;
    display: block;
  }
</style>